package com.lsy.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

	public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获得 {@number} 个月后的时间
	 * 
	 * @param strDate
	 *            字符串
	 * @param number
	 *            月数
	 * @return Calendar对象
	 */
	public static Calendar getNumberNext(String strDate, int number) {
		Calendar calendar = Calendar.getInstance();
		try {
			Date date = FORMAT.parse(strDate);
			calendar.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		calendar.add(Calendar.MONTH, number);
		return calendar;
	}

	/**
	 * 获得 {@number} 个月后的时间
	 * 
	 * @param date
	 *            时间
	 * @param number
	 *            月数
	 * @return Calendar对象
	 */
	public static Calendar getNumberNext(Date date, int number) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, number);
		return calendar;
	}

	/**
	 * 判断当前天数是否是月末
	 * 
	 * @return 是月末true，不是月末false
	 */
	public static boolean isLastDayOfMonth() {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		int lastDay = calendar.get(Calendar.DAY_OF_MONTH);
		return day == lastDay;
	}

	/**
	 * 判断指定 {@date} 时间是否是月末
	 * 
	 * @param date
	 *            指定时间
	 * @return 月末返回true
	 */
	public static boolean isLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		int lastDay = calendar.get(Calendar.DAY_OF_MONTH);
		return day == lastDay;
	}

	/**
	 * 获取当前时间的月
	 * 
	 * @return 月
	 */
	public static int getMonth() {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		return calendar.get(Calendar.MONTH);
	}

	/**
	 * 获取当前时间的天
	 * 
	 * @return 天
	 */
	public static int getDay() {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取指定 {@date} 时间的天
	 * 
	 * @param date
	 *            指定的时间
	 * @return 天
	 */
	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获得本月月末时间
	 * 
	 * @return Calendar对象
	 */
	public static Calendar getLastDayOfMonth() {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar;
	}

	/**
	 * 判断 {@calendar} 和当天是否是同一天（包含年份、月份都要相同）
	 * 
	 * @param calendar
	 *            给出的时间
	 * @return 是true
	 */
	public static boolean isToday(Calendar calendar) {
		Calendar today = Calendar.getInstance(Locale.CHINA);
		return (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
				&& calendar.get(Calendar.MONTH) == today.get(Calendar.MONTH)
				&& calendar.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH));
	}

	/**
	 * 判断{@calendar} 和当月是否是同一月（包含年份也要相同）
	 * 
	 * @param calendar
	 * @return
	 */
	public static boolean isSameMonth(Calendar calendar, Calendar calendarCampare) {
		return (calendar.get(Calendar.YEAR) == calendarCampare.get(Calendar.YEAR)
				&& calendar.get(Calendar.MONTH) == calendarCampare.get(Calendar.MONTH));
	}

	public static int getIntervalFromNow(Calendar calendar) {
		Calendar now = Calendar.getInstance(Locale.CHINA);
		return (calendar.get(Calendar.YEAR) - now.get(Calendar.YEAR)) * 12
				+ (calendar.get(Calendar.MONTH) - now.get(Calendar.MONTH));
	}
}
